package com.shopp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shopp.auto.JSONAuthentication;
import com.shopp.entity.ShFood;
import com.shopp.entity.UserZiliao;
import com.shopp.mapper.UserZiliaoMapper;
import com.shopp.response.Result;
import com.shopp.utils.UpPhotoNameUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shopp
 * @since 2022-03-04
 */
@RestController
@RequestMapping("/user-ziliao")
public class UserZiliaoController extends JSONAuthentication {
    @Autowired
    UserZiliaoMapper userZiliaoMapper;
    @ApiOperation(value = "添加资料")
    @PostMapping("/add")
    public void addZiLiao( HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int  userId, @RequestParam("phone") String phone, @RequestParam("address") String address,@RequestParam("name") String name) throws Exception {
        UserZiliao userZiliao=new UserZiliao();
        userZiliao.setUserId(userId);
        userZiliao.setPhone(phone);
        userZiliao.setAddress(address);
        userZiliao.setName(name);
        if (userZiliaoMapper.insert(userZiliao)> 0) {
            Result ok = Result.ok().message("添加成功");
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "删除资料")
    @PostMapping("/del")
    public void delZiLiao(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        int i = userZiliaoMapper.deleteById(id);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改用户电话")
    @PostMapping("/updUserPhone")
    public void updUserPhone(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int  id, @RequestParam("phone") String phone) throws Exception {
        UserZiliao userZiliao = userZiliaoMapper.selectById(id);
        userZiliao.setPhone(phone);
        if (userZiliaoMapper.updateById(userZiliao) > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "修改用户收货地址")
    @PostMapping("/updUserAddress")
    public void updUserAddress(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int  id, @RequestParam("address") String address) throws Exception {
        UserZiliao userZiliao = userZiliaoMapper.selectById(id);
        userZiliao.setAddress(address);
        if (userZiliaoMapper.updateById(userZiliao) > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改收货人员姓名")
    @PostMapping("/updUserName")
    public void updUserName(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int  id, @RequestParam("name") String name) throws Exception {
        UserZiliao userZiliao = userZiliaoMapper.selectById(id);
        userZiliao.setName(name);
        if (userZiliaoMapper.updateById(userZiliao) > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查看所有人的资料")
    @PostMapping("/LookAllZiLiao")
    public void LookAllZiLiao(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int  size, @RequestParam("current") int current) throws Exception {
        Page<UserZiliao> page=new Page<>(current,size);
        Page<UserZiliao> pages=userZiliaoMapper.selectPage(page,null);
        List<UserZiliao> records = pages.getRecords();
        int total=userZiliaoMapper.selectCount(null);
        if (records.size() > 0) {
            Result ok = Result.ok().data("ziliao",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error().message("暂无商品");
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "根据用户id查询资料")
    @PostMapping("/LookAllFoodByName")
    public void LookAllFoodByName(HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int  userId) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("user_id",userId);
        UserZiliao userZiliao=  userZiliaoMapper.selectOne(wrapper);
        if (userZiliao!=null) {
            Result ok = Result.ok().data("ziliao",userZiliao);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error().message("暂无资料，快去添加吧");
            this.WriteJSON(request, response, error);
        }
    }

}

